package ar.edu.unicen.pladema.vc1.fractal;

/**
 * Representación del fractal de Mandelbrot.
 * 
 * @author Sebastian Perruolo
 *
 */
public class Fractal2 {
	public static double REAL_MIN=-1.5;
	public static double REAL_MAX=0.5;
	public static double IMAG_MIN = -1;
	public static double IMAG_MAX = 1;
//	private int wArea;
//	private int hArea;
	private double xMin;
//	private double xMax;
	private double yMin;
//	private double yMax;
	private double wm;
	private double hm;
	private int maxIterations;
	
	public Fractal2(int wArea, int hArea,
			double xMin, double xMax,
			double yMin, double yMax,
			int maxIterations) {
		//this.wArea = wArea;
		//this.hArea = hArea;
		this.xMin = xMin;
//		this.xMax = xMax;
		this.yMin = yMin;
//		this.yMax = yMax;
		this.wm = (xMax - xMin) / wArea;
		this.hm = (yMax - yMin) / hArea;
		this.maxIterations = maxIterations;
	}

	public int doCount(int x, int y) {
		double dx = translateX(x);
		double dy = translateY(y);
		return iterationCount(dx, dy, maxIterations);
	}
	public double translateX(int x) {
		double dx = (new Double(x)).doubleValue();
		double rpart = dx * wm;
		return rpart + xMin;
	}
	public double translateY(int y) {
		double dy = (new Double(y)).doubleValue();
		double ipart = dy * hm;
		return ipart + yMin;
	}
	private static int iterationCount(double realPart, double imagPart,
			int maxIterations) {
		if (IMAG_MAX < imagPart) return -1;
		if (REAL_MAX < realPart) return -1;
		if (IMAG_MIN > imagPart) return -1;
		if (REAL_MIN > realPart) return -1;
		ComplexNumber z0 = new ComplexNumber(realPart,imagPart);
		ComplexNumber zn = new ComplexNumber(0,0);		
		int count = 0;//add by me
		double modulo = zn.module();
		while ((count<maxIterations) && ((modulo)<2)) {
			zn = z0.add(ComplexNumber.square(zn));
			modulo = zn.module();
			count++;
		}
		return (count); /// Esta es la variable que vamos a usar para colorear	
	}

}
